import Vue from 'vue'
const state = () => ({
    batteryData: {
        serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
        readUuid: '0783B03E-8535-B5A0-7140-A304D2495CB8',
        writeUuid: '0783B03E-8535-B5A0-7140-A304D2495CBA',
        deviceId: null,
        deviceName: '',
        connected: false,
        voltage: '', //电压
        range: '', //续航
        temperature: '', //温度
        batteryPercentage: '', //剩余电量
        packBatteryCount: '', //电池串数
        vehicleType: '', //车型
        vehicleTypeName: '',
        batteryCapacity: '', //电池容量
        consumption: '', //百公里电耗
        bluetoothName: '', //蓝牙名称
        softwareVersion: '', // 软件版本
        hardwareVersion: '', //硬件版本
        batteryType: '', //电池类型
        batteryTypeName: '', //电池类型名称
        curSourceId: '', //当前数据源
        protectBluetoothName: '', //保护蓝牙名称
        protectBluetoothMac: '', //保护蓝牙地址

    },
    vehicleTypeData: [],
    sourceData: [],
})

const getters = {
    batteryData: (state) => state.batteryData,
    vehicleTypeData: (state) => state.vehicleTypeData,
    sourceData: (state) => state.sourceData,
}

const mutations = {
    setBatteryData(state, source) {
        for (const key in source) {
            if (state.batteryData.hasOwnProperty(key)) {
                state.batteryData[key] = source[key];
            }
        }
    },
    setVehicleTypeData(state, data) {
        const exists = state.vehicleTypeData.some(item => item.value === data.value)
        if (!exists) {
            state.vehicleTypeData.push(data)
        }
    },
    setSourceData(state, sourceItem) {
        //查找是否存在 不在则添加
        const exists = state.sourceData.some(item => item.value === sourceItem.value)
        if (!exists) {
            state.sourceData.push(sourceItem)
        }
    },
    clearData(state) {
        state.batteryData = {
            serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',
            readUuid: '0783B03E-8535-B5A0-7140-A304D2495CB8',
            writeUuid: '0783B03E-8535-B5A0-7140-A304D2495CBA',
            deviceId: null,
            deviceName: '',
            connected: false,
            voltage: '', //电压
            range: '', //续航
            temperature: '', //温度
            batteryPercentage: '', //剩余电量
            packBatteryCount: '', //电池串数
            vehicleType: '', //车型
            vehicleTypeName: '',
            batteryCapacity: '', //电池容量
            consumption: '', //百公里电耗
            bluetoothName: '', //蓝牙名称
            softwareVersion: '', // 软件版本
            hardwareVersion: '', //硬件版本
            batteryType: '', //电池类型
            batteryTypeName: '', //电池类型名称
        }
        state.vehicleTypeData = []
        state.sourceData = []
    }
}

const actions = {
    /**
     * @description 电池模块信息 赋值
     * @param {*} { commit }
     */
    async setBatteryData({
        commit
    }, data) {
        await commit('setBatteryData', data)
    },

    async setVehicleTypeData({
        commit
    }, data) {
        await commit('setVehicleTypeData', data)
    },

    async setSourceData({
        commit
    }, sourceItem) {
        await commit('setSourceData', sourceItem)
    },
    async clearData({
        commit
    }, data) {
        await commit('clearData')
    }
}

export default {
    state,
    getters,
    mutations,
    actions
}